package com.miracle.system.web;

import com.miracle.common.utils.SecurityUtils;
import com.miracle.common.utils.StringUtils;
import com.miracle.system.base.domain.ResultMap;
import com.miracle.system.domain.SysResource;
import com.miracle.system.domain.TreeEntity;
import com.miracle.system.service.SysResourceService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：miracle.cui
 * @description：TODO
 * @date ：2020/8/10 15:46
 */
@ApiModel(value = "资源管理类")
@RestController
@RequestMapping("/system/resource")
public class SysResourceController {

    @Autowired
    SysResourceService resourceService;

    @ApiOperation(value = "所用资源列表")
    @GetMapping("/all")
    public ResultMap all(SysResource resource){

        List<SysResource> resourceList = resourceService.selectResourceList(resource);
        return ResultMap.success(resourceList);
    }

    @ApiOperation(value = "资源列表")
    @GetMapping("/list")
    public ResultMap list(Integer parentResourceId){

        List<SysResource> resourceList = resourceService.selectResourceByParentResourceId(parentResourceId);
        return ResultMap.getPage(resourceList);
    }

    @ApiOperation(value = "资源树")
    @GetMapping("/treeSelect")
    public ResultMap trees(TreeEntity treeEntity){

        SysResource resource = new SysResource();
        resource.setParentResourceId(treeEntity.getId());
        List<SysResource> resourceList = resourceService.selectResourceList(resource);

        return ResultMap.success(resourceService.buildResourceTree(resourceList));
    }

    @ApiOperation(value = "新增资源")
    @PostMapping
    public ResultMap add(@Validated @RequestBody SysResource resource){

        resource.setCreateBy(SecurityUtils.getUserId());

        Integer rows = resourceService.insertResource(resource);
        return rows > 0? ResultMap.success(resource) : ResultMap.error();
    }

    @ApiOperation(value = "修改资源")
    @PutMapping()
    public ResultMap edit(@Validated @RequestBody SysResource resource){

        resource.setLastUpdateBy(SecurityUtils.getUserId());

        Integer rows = resourceService.updateResource(resource);
        return rows > 0?ResultMap.success() : ResultMap.error();
    }

    @ApiOperation(value = "查询资源")
    @GetMapping(value = {"/", "/{resourceId}"})
    public ResultMap getInfo(@PathVariable(value = "resourceId", required = false) Integer resourceId){

        if (StringUtils.isNotNull(resourceId)){
            return ResultMap.success(resourceService.selectResourceByResourceId(resourceId));
        } else {
            return ResultMap.error();
        }
    }

    @ApiOperation(value = "删除资源")
    @DeleteMapping
    public ResultMap delete(@Validated @RequestBody Integer[] resourceIds){

        Integer rows = resourceService.deleteResourceByResourceIds(resourceIds);
        return rows > 0 ? ResultMap.success() : ResultMap.error();
    }

    /* 远程校验 */
    @ApiOperation(value = "远程校验资源")
    @GetMapping(value = {"/remoteCheckResource"})
    public ResultMap remoteCheckResource(SysResource resource){
        resource.setStatus("enabled");
        SysResource checkResource = resourceService.remoteCheckResource(resource);
        return ResultMap.success(checkResource);
    }
}
